// pages/post/edit_post/edit_post.js
let util = require('../../../utils/content_check.js')
var app = getApp()
var total = [];
var tempFilePaths;
var uid;
Page({

  data: {
    imageUrl: [],
    _num:3,
    suggestsList:[],
    isOverdue: null,
    isExchanged: null,
    articleid:null,
    title: '',
    content: '',
    statusflag:'',
    theme:null
  },

  // 选择图片
  chooseImg: function(){
    var that = this;
    wx.chooseImage({
      count: 3, 
      sizeType: ['original', 'compressed'], 
      sourceType: ['album', 'camera'],
      success: function (res) {
        tempFilePaths = res.tempFilePaths
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.uploadFile2(tempFilePaths, 0);
        var uploadedImageLists = that.data.imageUrl;
        var totalImages = uploadedImageLists.concat(res.tempFilePaths);
        var aNum = 0;
        if (totalImages.length >= 3) {
          totalImages.length = 3;
          aNum = 1;
        }
        that.setData({
          num: 0,
          addNum: aNum,
          imageUrl: totalImages
        })
      }
    })
  },
  uploadFile2: function (file, i) {//递归调用
    var that = this;
    wx.uploadFile({
      url: app.getHeader() +'/SheQu/uploadWxImg', //仅为
      filePath: file[i],
      name: 'img',
      success: function (res) {
        var obj = new Object();
        obj.ind = i + 1;
        var data = res.data;
        console.info(data);
        obj.src = data;
        console.info("--------------*****-------------------");
        console.info(obj);
        if (!((i + 1) == file.length)) {
          total.push(obj);
          that.uploadFile2(file, i + 1);
        } else {
          total.push(obj);
          that.setData({ perImgSrc: total });
        }
      }
    })
  },
  removeImage: function(e){
    var that = this;
    var currentImageLists = that.data.imageUrl; 
    currentImageLists.splice(e.target.dataset.index,1);
    that.setData({
        imageUrl:currentImageLists
    })
    if(currentImageLists.length < 3 && currentImageLists.length >= 1) {
        that.setData({
            addNum: 0
        })
    } else if(currentImageLists.length == 0) {
        that.setData({
            addNum: 1,
            num: 1
        })
    }
  },
  preImage:function(e){
    var that = this;
    var currentImageUrl = that.data.imageUrl[e.target.dataset.index]
    wx.previewImage({
      current: currentImageUrl, // 当前显示图片的http链接
      urls: that.data.imageUrl // 需要预览的图片http链接列表
    })
  },

  onTitleInput: function (e) {
    var title = e.detail.value;
    this.setData({
      title: title
    })
  },
  onContentInput: function (e) {
    var content = e.detail.value;
    this.setData({
      content: content
    })
  },

  onExchangedChange: function (e){
     // 在theme==1 闲物置换 0 默认 1 未置换 2 已置换 3 已过期
    var isExchanged = null;
    if(e.detail.value=="false"){
      isExchanged = 1
    }else{
      isExchanged = 2
    }
    this.setData({
      statusflag:isExchanged 
    })
  },
  onOverdueChange: function (e){
    // 在theme==2 家政服务 0 默认 1 未置换 2 已置换 3 已过期
    var isOverdue = null;
    if(e.detail.value=="false"){
      isOverdue = 0
    }else{
      isOverdue = 1
    }
    this.setData({
      statusflag: isOverdue
    })
  },

  onLoad: function (options) {
    var thisarticleid = options.post_id
    // var posts_list = wx.getStorageSync('posts_list')
    // var post = null
    // for(var i=0; i<posts_list.length; i++){
    //   if(posts_list[i].post_id == post_id){
    //     post = posts_list[i]
    //     break
    //   }
    // }
    this.setData({
      articleid:thisarticleid
    })
    app.sendGetRequest('/bbs/'+thisarticleid,(res) => {
      console.log("res:",res)
      this.setData({
        theme:res.theme,
        statusflag:res.statusflag,
        title:res.title,
        content:res.content
      })
    })
    
  },

  // 整理数据，提交到缓存，试图写入js文件内
  formSubmit: async function (e){
    var title = this.data.title
    var content = this.data.content
    if(!title){
      wx.showToast({
        title: '请输入标题',
        icon: 'none',
        duration: 2000//持续的时间
      })
      return
    }
    if(!content){
      wx.showToast({
        title: '请输入内容',
        icon: 'none',
        duration: 2000//持续的时间
      })
      return
    }
    // var isChecked = await util.checkString(title+content)
    // if(!isChecked){
    //   wx.showToast({
    //     icon: 'none',
    //     title: '文字违规',
    //   })
    //   this.setData({
    //     title: this.data.post.title,
    //     content: this.data.post.content
    //   })
    //   return
    // }else{
    //   this.data.post.title = title
    //   this.data.post.content = content
    //   // var time = []
    //   // var myDate = new Date()
    //   // time.push(myDate.getFullYear(), myDate.getMonth(), myDate.getDate(), myDate.getHours(), myDate.getMinutes(), myDate.getSeconds())
    //   // this.data.post.time = time
    //   this.data.post.preview = content.substr(0, 40)
    //   this.data.post.isOverdue = this.data.isOverdue
    //   this.data.post.isExchanged = this.data.isExchanged
    //   // console.log(this.data.post)
    //   // this.data.post.post_pic = this.data.imageUrl
    //   // var post = {
    //   //   post_id: post_id,
    //   //   author_id: author_id,
    //   //   author_name: author_name,
    //   //   author_pic: author_pic,
    //   //   time: time,
    //   //   title: title,
    //   //   preview: preview,
    //   //   content: content,
    //   //   category: category,
    //   //   isOverdue: isOverdue,
    //   //   isExchanged: isExchanged,
    //   //   post_pic: post_pic,
    //   //   comment_list: comment_list
    //   // }
      
    //   var posts_list = wx.getStorageSync('posts_list')
    //   for(var i=0; i<posts_list.length; i++){
    //     if(posts_list[i].post_id == this.data.post.post_id){
    //       posts_list[i] = this.data.post
    //       break
    //     }
    //   }
    //   // console.log(posts_list)
    //   wx.setStorageSync('posts_list', posts_list)
    //   // console.log(wx.getStorageSync('posts_list'))
    //   wx.navigateTo({
    //     url: '../post_content/post_content?post_id=' + this.data.post.post_id,
    //   })
    // }
    var updateInfo = {
      articleid:this.data.articleid,
      title:this.data.title,
      content:this.data.content,
      statusflag:this.data.statusflag
    }
    var that = this
    app.sendPutRequest('/bbs',updateInfo,(res)=>{
      if(res.code == 200){
        wx.showToast({
          title: '修改成功!',
          icon: 'success',
          duration: 2000,
          success:function(){
            setTimeout(function(){
              wx.navigateTo({
                    url: '../post_content/post_content?articleid=' + that.data.articleid,
                  })
            })
          }
        })
        
      }
    })
  }

})